package com.wyk.redis;

import java.io.Serializable;
import java.util.Collection;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;

import com.wyk.cache.JedisShiroSessionRepository;

public class RedisSessionDao extends AbstractSessionDAO{
	
	private JedisShiroSessionRepository jedisShiroSessionRepository;
		
	public JedisShiroSessionRepository getJedisShiroSessionRepository() {
		return jedisShiroSessionRepository;
	}

	public void setJedisShiroSessionRepository(JedisShiroSessionRepository jedisShiroSessionRepository) {
		this.jedisShiroSessionRepository = jedisShiroSessionRepository;
	}

	@Override
	public void delete(Session session) {
		if (session == null) {  
        	System.out.println("session不能为null");
            return;  
        } 
		 Serializable id = session.getId();  
	     if (id != null)  
	    	 jedisShiroSessionRepository.deleteSession(id);  
	}

	@Override
	public Collection<Session> getActiveSessions() {
		return jedisShiroSessionRepository.getAllSessions();
	}

	@Override
	public void update(Session session) throws UnknownSessionException {
		jedisShiroSessionRepository.saveSession(session);
	}

	@Override
	protected Serializable doCreate(Session session) {
		 	Serializable sessionId = this.generateSessionId(session);  
	        this.assignSessionId(session, sessionId);  
	        jedisShiroSessionRepository.saveSession(session);  
	        return sessionId;  
	}

	@Override
	protected Session doReadSession(Serializable serializable) {
		return jedisShiroSessionRepository.getSession(serializable);
	}
}
